package com.zfc.threadpool.service.impl;

import com.zfc.threadpool.entity.ThreadPoolConfigEntity;
import com.zfc.threadpool.service.IDynamicThreadPoolService;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author zfc
 * @version 1.0
 */
public class DynamicThreadPoolServiceImpl implements IDynamicThreadPoolService {

    private final Logger logger = LoggerFactory.getLogger(DynamicThreadPoolServiceImpl.class);
    private final String applicationName;
    private final Map<String, ThreadPoolExecutor> threadPoolExecutorMap;
    public DynamicThreadPoolServiceImpl(String applicationName, Map<String, ThreadPoolExecutor> threadPoolExecutorMap) {
        this.applicationName = applicationName;
        this.threadPoolExecutorMap = threadPoolExecutorMap;
    }

    /**
     * 查找线程池列表
     * @return 线程池的列表数目
     */

    @Override
    public List<ThreadPoolConfigEntity> queryThreadPoolsList() {
        Set<String> threadPoolBeanNames = threadPoolExecutorMap.keySet();
        List<ThreadPoolConfigEntity> vos= new ArrayList<>(threadPoolBeanNames.size());
        for (String beanName : threadPoolBeanNames) {
            ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(beanName);
            ThreadPoolConfigEntity vo = new ThreadPoolConfigEntity(applicationName, beanName);
            vo.setCorePoolSize(threadPoolExecutor.getCorePoolSize());
            vo.setMaximumPoolSize(threadPoolExecutor.getMaximumPoolSize());
            vo.setActiveCount(threadPoolExecutor.getActiveCount());
            vo.setPoolSize(threadPoolExecutor.getPoolSize());
            vo.setQueueType(threadPoolExecutor.getQueue().getClass().getSimpleName());
            vo.setQueueSize(threadPoolExecutor.getQueue().size());
            vo.setRemainingCapacity(threadPoolExecutor.getQueue().remainingCapacity());
            vos.add(vo);
        }
        return vos;
    }

    /**
     * 根据线程池名字查找线程池
     * @param threadPoolName 线程池名字
     * @return 返回线程池对象
     */

    @Override
    public ThreadPoolConfigEntity queryThreadPoolConfigByName(String threadPoolName) {
        ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolName);
        if (null == threadPoolExecutor) return new ThreadPoolConfigEntity(applicationName, threadPoolName);
        ThreadPoolConfigEntity vo = new ThreadPoolConfigEntity(applicationName, threadPoolName);

        vo.setMaximumPoolSize(threadPoolExecutor.getMaximumPoolSize());
        vo.setActiveCount(threadPoolExecutor.getActiveCount());
        vo.setPoolSize(threadPoolExecutor.getPoolSize());
        vo.setQueueType(threadPoolExecutor.getQueue().getClass().getSimpleName());
        vo.setQueueSize(threadPoolExecutor.getQueue().size());
        vo.setRemainingCapacity(threadPoolExecutor.getQueue().remainingCapacity());

        if (logger.isDebugEnabled()) {
            logger.info("动态线程池，配置查询 应用名:{} 线程名:{} 池化配置:{}", applicationName, threadPoolName, JSON.toJSONString(vo));
        }
        return vo;
    }

    @Override
    public void updateThreadPoolConfig(ThreadPoolConfigEntity threadPoolConfigEntity) {
        if (null == threadPoolConfigEntity || !applicationName.equals(threadPoolConfigEntity.getAppName())) return;
        ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolConfigEntity.getThreadPoolName());
        if (null == threadPoolExecutor) return;

        // 设置参数 「调整核心线程数和最大线程数」
        threadPoolExecutor.setCorePoolSize(threadPoolConfigEntity.getCorePoolSize());
        threadPoolExecutor.setMaximumPoolSize(threadPoolConfigEntity.getMaximumPoolSize());


    }
}
